﻿/**
 * This file contains the class for the mathematical puzzle. Upon initialisation
 * it will automatically generate a random equation for the user to solve and update the
 * text display on the device accordingly.
 * */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Scheduler;

namespace PhoneApp1
{
    public partial class MathPuzzle : PhoneApplicationPage
    {
        public MathPuzzle()
        {
            InitializeComponent();
        }

        private int mathPuzzleAnswer = 0;

        // Called when the user presses the "START" button and hides the start button and then
        // accordingly initialises the math puzzle
        private void btnStartMathPuzzle_Click(object sender, RoutedEventArgs e)
        {
            btnStartMathPuzzle.Visibility = System.Windows.Visibility.Collapsed;
            initMathPuzzle();
        }

        private void initMathPuzzle()
        {
            // Format of the equation is as follows: (x + x) * x
            // First of all format a new mathematical equation
            Random random = new Random();

            
            int firstAddVal = random.Next(50);
            int secondAddVal = random.Next(50);
            int thirdTimesVal = random.Next(5) + 1; // the +1 makes sure it's value is 1-5 and never 0.

            // Okay now update the mathPuzzleAnswer variable so we can check the answer using getMathPuzzleAnswer later
            mathPuzzleAnswer = (firstAddVal + secondAddVal) * thirdTimesVal;

            // Right now format the string and update the text accordingly
            string equation = "(" + firstAddVal + " + " + secondAddVal + ")" + " * " + thirdTimesVal;

            // Update the text to show this equation
            mathEquationText.Text = equation;

            // Reset the text in the input field
            answerInputBox.Text = "";

            // Right now make them all visible!
            mathEquationText.Visibility = System.Windows.Visibility.Visible;
            answerInputBox.Visibility = System.Windows.Visibility.Visible;
            equalsText.Visibility = System.Windows.Visibility.Visible;
            SubmitBttn.Visibility = System.Windows.Visibility.Visible;
        }

        // Okay now check for input if the user answers the correct answer we'll go to the main page
        // otherwise generate a new puzzle.

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            // Error checking to ensure the user has
            // not entered a bad
            if (answerInputBox.Text.Length < 1)
            {
                return;
            }
            
            int input = Convert.ToInt32(answerInputBox.Text);

            // If the answer is not correct the user must try again!
            if (input != getMathPuzzleAnswer())
            {
                MessageBox.Show("Wrong Answer!! Try again!");
                initMathPuzzle();
                return;
            }

            // Yey, the answer is correct!
            // Disable the alarm here.
            // Make sure to add a null check here - otherwise the following issue will be shown:
            // "A first chance exception of type 'System.NullReferenceException' occurred in PhoneApp1.dll"
            // See issue #18 for more details

            // Disable all alarms if the puzzle is solved!
            for (int i = 0; i < 50; i++)
            {
                ScheduledAction a1 = ScheduledActionService.Find("student_alarm_clock_" + i);
                if (a1 != null)
                {
                    ScheduledActionService.Remove("student_alarm_clock_" + i);
                }
            }

            // Show that the alarm is deactivated.
            MessageBox.Show("ALARM DEACTIVATED");

            // Now navigate back to the main page
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        // return an integer representing a mathematical equation
        // for the correct answer.
        private int getMathPuzzleAnswer()
        {
            return mathPuzzleAnswer;
        }

    }
}